package com.tangula.web.form;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import com.tangula.web.kashine.entity.PagableForm;

public class DefaultPagingBody<T> implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5490958266563346761L;
	
	private int pageIndex;
	private int pageSize;
	private int total;
	private List<T> items;
	
	
	
	public DefaultPagingBody() {
		super();
	}
	
	
	public DefaultPagingBody(int index, int size){
		this();
		this.pageIndex = index;
		this.pageSize = size;
	}
	
	public DefaultPagingBody(PagableForm pageInfo){
		this();
		this.pageIndex = pageInfo.getIndex();
		this.pageSize = pageInfo.getSize();
	}
	
	public DefaultPagingBody(int pageIndex, int pageSize, int total, List<T> items) {
		super();
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
		this.total = total;
		this.items = items;
	}
	
	public int getPageIndex() {
		return pageIndex;
	}
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public List<T> getItems() {
		return items;
	}
	public void setItems(List<T> items) {
		this.items = items;
	}

	
	public PageRequest toPageRequest(Sort.Order ... orders) {
		return PageRequest.of(pageIndex - 1,pageSize,
				Sort.by(orders));
	}
	
	public void fill(Page<T> page) {
		if(page!=null) {
			this.items = page.getContent();
			this.total = (int)page.getTotalElements();
		}
	}

}
